/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { BuildArtifactConst, BuildDirConst } from '../../const/build-directory-const.js';
import { ConfigJson } from '../../options/configure/config-json-options.js';
import { AbstractProcessResourceIncrement } from '../abstract/abstract-process-resource-increment.js';
import { TaskNames } from '../common/task-names.js';
import { TargetTaskService } from '../service/target-task-service.js';

import { getTaskRealName } from './hook/legacy-hook-compile-resource.js';
import LegacyFATask = TaskNames.LegacyFATask;

import path from 'path';

/**
 * FA 模型用于处理和生成Restool增量方式的编译、链接命令参数
 *
 * @since 2022/09/22
 */
export class LegacyProcessResourceIncrement extends AbstractProcessResourceIncrement {
  constructor(taskService: TargetTaskService, entryModuleName?: string) {
    super(taskService, getTaskRealName(LegacyFATask.PREVIEW_PROCESS_RESOURCE, entryModuleName));
    this.entryModuleName = entryModuleName;
    this.outputDir = path.resolve(this.pathInfo.getIntermediatesRes(), this.entryModuleName ?? '');
  }

  initCommandBuilder() {
    const configObj = this.moduleModel.getJsonObjByTargetName(this.targetName);
    const bundleName = (configObj as ConfigJson.ConfigOptObj).app.bundleName;
    this.restoolCommandBuilder = this.getRestoolCommandBuilder();

    this.restoolCommandBuilder
      .addJsonFile(this.processedJson!) // config.json文件
      .addModulePackName(bundleName) // bundleName
      .addOutputDir(this.outputDir!) // 输出目录
      .addResTable(path.resolve(this.getTaskTempDir(this.targetData, BuildDirConst.R), BuildArtifactConst.RESOURCE_TABLE_H))
      .incrementalCompile();

    // HOS工程会单独传递HOS toolchain路径给restool
    if (!this.sdkInfo.isOhos) {
      this.restoolConfigBuilder.addDefinedSysIds(this.sdkInfo.getHosToolchainsDir());
    }
  }
}
